﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ExchangeHog
{
	public class EHConfig : System.ComponentModel.INotifyPropertyChanged
	{
		string filename;
		internal string FileName { get { return filename; } set { filename = value; OnPropertyChanged("Title"); } }
		public string Title { get { return filename != null ? "ExchangeHog - " + filename : "ExchangeHog"; } }

		string exchangeUrl;
		public String ExchangeUrl { get { return exchangeUrl; } set { exchangeUrl = value; OnPropertyChanged("ExchangeUrl"); } }
		string exchangeUserid;
		public String ExchangeUserid { get { return exchangeUserid; } set { exchangeUserid = value; OnPropertyChanged("ExchangeUserid"); } }
		System.Security.SecureString exchangePassword;
		public String ExchangePassword
		{
			get
			{
				IntPtr ipPassword = new IntPtr();
				ipPassword = System.Runtime.InteropServices.Marshal.SecureStringToBSTR(exchangePassword);
				string sPassword = System.Runtime.InteropServices.Marshal.PtrToStringBSTR(ipPassword);
				System.Runtime.InteropServices.Marshal.ZeroFreeBSTR(ipPassword);
				return sPassword;
			}
			set
			{
				exchangePassword = new System.Security.SecureString();
				foreach (Char ch in value.ToCharArray())
					exchangePassword.AppendChar(ch);
				OnPropertyChanged("ExchangePassword");
			}
		}
		public System.Security.SecureString SSExchangePassword { get { return exchangePassword; } }

		public System.Collections.ObjectModel.ObservableCollection<Pop3Config> Pop3s { get; set; }

		public EHConfig()
		{
			FileName = null;
			ExchangeUrl = "https://xxx.xxx.com/EWS/Exchange.asmx";
			ExchangeUserid = "userid";
			ExchangePassword = "";
			Pop3s = new System.Collections.ObjectModel.ObservableCollection<Pop3Config>();
		}

		#region INotifyPropertyChanged Members

		public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

		#endregion

		private void OnPropertyChanged(String info)
		{
			System.ComponentModel.PropertyChangedEventHandler handler = PropertyChanged;
			if (handler != null)
			{
				handler(this, new System.ComponentModel.PropertyChangedEventArgs(info));
			}
		}

		internal void Save(string filename)
		{
			using (System.Xml.XmlWriter xw = System.Xml.XmlWriter.Create(filename))
			{
				xw.WriteStartElement("eh");
				xw.WriteStartElement("exchange");
				xw.WriteStartElement("url");
				xw.WriteString(ExchangeUrl);
				xw.WriteEndElement(); // url
				xw.WriteStartElement("userid");
				xw.WriteString(ExchangeUserid);
				xw.WriteEndElement(); // userid
				xw.WriteStartElement("password");
				xw.WriteString(MainWindow.Encrypt(ExchangePassword, "ehCuCa".Replace('C', 'k'), true));
				xw.WriteEndElement(); // password
				xw.WriteEndElement(); // exchange
				xw.WriteStartElement("feeds");
				foreach (Pop3Config pop3config in Pop3s)
				{
					xw.WriteStartElement("pop3");
					xw.WriteStartElement("url");
					xw.WriteString(pop3config.Server);
					xw.WriteEndElement(); // url
					xw.WriteStartElement("port");
					xw.WriteString(pop3config.Port.ToString());
					xw.WriteEndElement(); // port
					xw.WriteStartElement("userid");
					xw.WriteString(pop3config.Userid);
					xw.WriteEndElement(); // userid
					xw.WriteStartElement("password");

					//IntPtr ipPassword = new IntPtr();
					//ipPassword = System.Runtime.InteropServices.Marshal.SecureStringToBSTR(pop3config.Password);
					//string sPassword = System.Runtime.InteropServices.Marshal.PtrToStringBSTR(ipPassword);
					//System.Runtime.InteropServices.Marshal.ZeroFreeBSTR(ipPassword);

					xw.WriteString(MainWindow.Encrypt(pop3config.Password, "ehCuCa".Replace('C', 'k'), true));
					xw.WriteEndElement(); // password
					xw.WriteStartElement("ssl");
					xw.WriteString(pop3config.SSL ? "yes" : "no");
					xw.WriteEndElement(); // ssl
					xw.WriteEndElement(); // pop3
				}
				xw.WriteEndElement(); // feeds
				xw.WriteEndElement(); // eh
			}
		} // end of Save

		internal void Load(string filename)
		{
			using (System.Xml.XmlReader xr = System.Xml.XmlReader.Create(filename))
			{
				FileName = filename;

				xr.Read();
				xr.ReadStartElement("eh");
				xr.ReadStartElement("exchange");
				if (!xr.IsEmptyElement)
				{
					xr.ReadStartElement("url");
					ExchangeUrl = xr.ReadString();
					xr.ReadEndElement(); // url
				}
				else
				{
					ExchangeUrl = "";
					xr.ReadElementString("url");
				}
				if (!xr.IsEmptyElement)
				{
					xr.ReadStartElement("userid");
					ExchangeUserid = xr.ReadString();
					xr.ReadEndElement(); // userid
				}
				else
				{
					ExchangeUserid = "";
					xr.ReadElementString("userid");
				}
				if (!xr.IsEmptyElement)
				{
					xr.ReadStartElement("password");
					ExchangePassword = MainWindow.Decrypt(xr.ReadString(), "ehCuCa".Replace('C', 'k'), true);
					xr.ReadEndElement(); // password
				}
				else
				{
					ExchangePassword = "";
					xr.ReadElementString("password");
				}
				xr.ReadEndElement(); // exchange
				xr.ReadStartElement("feeds");
				Pop3s.Clear();
				do
				{
					xr.ReadStartElement("pop3");
					Pop3Config pop3config = new Pop3Config();
					Pop3s.Add(pop3config);

					if (!xr.IsEmptyElement)
					{
						xr.ReadStartElement("url");
						pop3config.Server = xr.ReadString();
						xr.ReadEndElement(); // url
					}
					else
						xr.ReadElementString("url");
					if (!xr.IsEmptyElement)
					{
						xr.ReadStartElement("port");
						pop3config.Port = Int16.Parse(xr.ReadString());
						xr.ReadEndElement(); // port
					}
					else
						xr.ReadElementString("port");
					if (!xr.IsEmptyElement)
					{
						xr.ReadStartElement("userid");
						pop3config.Userid = xr.ReadString();
						xr.ReadEndElement(); // userid
					}
					else
						xr.ReadElementString("userid");
					if (!xr.IsEmptyElement)
					{
						xr.ReadStartElement("password");
						pop3config.Password = MainWindow.Decrypt(xr.ReadString(), "ehCuCa".Replace('C', 'k'), true);
						xr.ReadEndElement(); // password
					}
					else
						xr.ReadElementString("password");
					if (!xr.IsEmptyElement)
					{
						xr.ReadStartElement("ssl");
						pop3config.SSL = String.Compare(xr.ReadString().Trim(), "yes", true) == 0; // ignore case
						xr.ReadEndElement(); // ssl
					}
					else
						xr.ReadElementString("ssl");
					xr.ReadEndElement(); // pop3
				} while (xr.IsStartElement("pop3"));
				xr.ReadEndElement(); // feeds
				xr.ReadEndElement(); // eh
			}
		} // end of Load
	} // end of EHConfig
}
